package com.ytkj.tradeserver.controller.pushMessage;

import com.github.pagehelper.PageHelper;
import com.ytkj.tradeserver.constant.ComConstants;
import com.ytkj.tradeserver.entity.PushMessageInfoVO;
import com.ytkj.tradeserver.entity.UserInfo;
import com.ytkj.tradeserver.model.PageBean;
import com.ytkj.tradeserver.service.iservice.IPushMessageDataService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by ccm on 2017/3/3.
 * 用了推送发布信息通知
 */
@Controller
@RequestMapping("/pushMessage")
public class PushMessageDataController {

    private static Logger logger = Logger.getLogger(PushMessageDataController.class);

    @Resource
    IPushMessageDataService pushMessageDataService;

    //跳转至发布信息页面
    @RequestMapping("/returnPushMessage")
    public String goPushMessage(){
        return "pushMessage/addPushMessage";
    }

    @RequestMapping(value="/addPushMessage",method = RequestMethod.POST)
    public String addPushMessage(PushMessageInfoVO messageInfoVO, HttpServletRequest request){
        UserInfo userInfo = (UserInfo)request.getSession(true).getAttribute("userInfo");
        messageInfoVO.setCreator(userInfo.getId());
        messageInfoVO.setLastUpdateUser(userInfo.getId());
        pushMessageDataService.savePushMessageData(messageInfoVO);
        logger.info("end addPushMessage id: " + messageInfoVO.getId());
        return  "redirect:/pushMessage/queryAllPushMessage";
    }

    @RequestMapping(value="/queryAllPushMessage",method = RequestMethod.GET)
    public ModelAndView queryAllPushMessage(
            @RequestParam(value="pageNum",required = false,defaultValue= ComConstants.PAGENUM) int pageNum,
            @RequestParam(value="pageSize",required = false,defaultValue=ComConstants.PAGESIZE) int pageSize,
            PushMessageInfoVO message, HttpServletRequest request){
        String url = request.getContextPath() + request.getServletPath();
        PageBean<PushMessageInfoVO> messageList = pushMessageDataService.queryAllPushMessage(url,message,new PageBean(pageNum,pageSize));
        return new ModelAndView("pushMessage/pushMessageList","pageBean",messageList);
    }

}
